#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework4_8.py
# author:wmf
# datetime:2021/4/12 19:59
# software: PyCharm
'''
this is function description
'''
# import module your need

# 8 京东二面笔试题
# 1） 生成一个大文件ip.txt,要求1200行，每行随机为172.25.254.1---172.25.254.254之间的一个ip地址;
# 2） 读取ip.txt文件统计这个文件中ip出现频率排前10的ip

from random import randint


def make_file():
    with open('ip.txt','w') as f:
        for i in range(0,1200):
            id = '172.25.254.'
            x = str(randint(1,254))
            f.write(id + x)
            f.write('\n')


def ip_count():
    count = {}
    with open('ip.txt','r') as f:
        for x in f:
            x = x.strip('\n')  # 去除换行符
            if x in count:
                count[x] = count[x] + 1
            else:
                count[x] = 1
    list1 = sorted(count.items(), key=lambda item: item[1], reverse=True)
    print("出现频率前十的ip为:")
    for x in range(10):
        print(list1[x])


make_file()
ip_count()
